/**
* Copyright (c) 2016-2100 yunclouds
*/
package com.yunqian.web;

import com.yunqian.redot.constant.Operation;
import com.yunqian.redot.constant.OperationMenus;
import com.yunqian.redot.constant.OrderStatus;
import com.yunqian.redot.domain.CoursesOrder;
import com.yunqian.redot.domain.dto.CoursesOrderDto;
import com.yunqian.redot.service.CoursesOrderService;
import com.yunqian.redot.service.OperationLogService;
import com.yunqian.util.bean.BeanUtil;
import com.yunqian.util.poi.PoiExcelExport;
import com.yunqian.util.poi.ServletUtil;
import com.yunqian.util.validate.Validator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * Describe: 客户课程报名记录控制器
 *
 * @author zsy
 * 2018-09-29 15:54:11
 */
 @Controller
 @RequestMapping("/api/coursesOrder")
public class CoursesOrderController {

     @Autowired
     private CoursesOrderService coursesOrderService;
     @Autowired
     private OperationLogService operationLogService;

    /**
     * 获取分页
     *
     * @param params
     * @return
     */
    @ResponseBody
    @PostMapping("/pagination")
    public Page<CoursesOrder> pagination(@RequestBody Map<String, Object> params) {
        return coursesOrderService.pagination(params);
    }

    /**
     * 导出excel
     *
     * @param req
     * @param resp
     */
    @RequestMapping("/export")
    public void export(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("id");
        operationLogService.save(OperationMenus.SIGN_ORDER, Operation.EXPORT, Long.valueOf(id));
        // 根据时间范围搜索数据
        String orderNo = req.getParameter("orderNo");
        String phone = req.getParameter("phone");
        String beginTime = req.getParameter("beginTime");
        String endTime = req.getParameter("endTime");
        if(Validator.isNull(beginTime)){
            beginTime = "2018-01-01 00:00:00";
        }
        if(Validator.isNull(endTime)){
            endTime = "2050-12-30 23:59:59";
        }
        List<CoursesOrder> list = coursesOrderService.getByOrderNoAndPhoneAndRangeTime(
                orderNo,
                phone,
                beginTime,
                endTime);
        // 构造显示字段
        List<CoursesOrderDto> copyList = new ArrayList<>();
        list.forEach(item -> {
            CoursesOrderDto dto = new CoursesOrderDto();
            BeanUtil.copyNotNullProperties(dto, item);
            dto.setTitle(item.getCourses().getTitle());
            String sexStr = item.getSex().equals(1) ? "男":"女";
            dto.setSexStr(sexStr);
            String statusStr = "";
            if(OrderStatus.NEW.getCode().equals(item.getStatus())){
                statusStr = "待支付";
            }else if(OrderStatus.SUCCESS.getCode().equals(item.getStatus())){
                statusStr = "报名成功";
            }else {
                statusStr = "报名失败";
            }
            dto.setStatusStr(statusStr);
            String dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(item.getSaveTime());
            dto.setSaveTime(dateStr);
            copyList.add(dto);
        });

        // 执行导出
        String fileName = new SimpleDateFormat("yyyyMMdd").format(new Date()) + "课程报名清单" + ".xls";
        ServletUtil su = new ServletUtil(fileName, req, resp);
        su.poiExcelServlet();
        String[] heads = {"序号", "订单号", "课程名称", "姓名", "年龄", "性别", "联系电话", "邮箱", "工作单位", "省份", "市区", "订单状态", "金额", "下单时间"};
        String[] cols = {"id", "orderNo", "title", "name", "age", "sexStr", "phone", "email", "company", "province", "city", "statusStr", "price", "saveTime"};
        // 这里传第几个字段是数字，从0开始
        int[] numerics = {0,4,6};
        ServletUtil suResp = new ServletUtil(resp);
        PoiExcelExport<CoursesOrder> pee = new PoiExcelExport(fileName, heads, cols, copyList, numerics, suResp.getOut());
        pee.exportExcel();
    }
}
